<template>
  <div class="app-container">
    <div class="header">
      <el-form :inline="true" :model="searForm" ref="ruleFormRef">
        <el-form-item label="日期">
          <el-date-picker
            v-model="searForm.month"
            type="month"
            format="YYYY-MM"
            placeholder="请选择"
            value-format="YYYY-MM"
          />
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="searForm.content" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :icon="Search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      <div class="table-inner">
        <el-table v-loading="loading" row-key="id" :data="list" style="width: 100%" border>
          <template #empty>
            <el-empty description="暂无数据" />
          </template>
          <el-table-column prop="admin_id" label="操作用户" align="center" />
          <el-table-column prop="ip" label="IP" align="center" />
          <el-table-column prop="url" label="URL" align="center" />
          <el-table-column prop="method" label="操作方法" align="center" width="100" />
          <el-table-column
            prop="useragent"
            label="useragent"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="content"
            label="返回内容"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column prop="create_time" label="创建时间" align="center" width="180" />
        </el-table>
      </div>
      <div class="pagination">
        <el-pagination
          v-model:currentPage="query.page"
          :page-size="10"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Search } from '@element-plus/icons-vue'
import { nextTick, onMounted, reactive, ref } from 'vue'
import { systemLog } from '@/api/admin'
const loading = ref(true)
const list = ref()
const total = ref(0)
const query = reactive({
  page: 1,
  limit: 10,
})
const searForm = reactive({
  month: '',
  content: '',
})
const getList = () => {
  loading.value = true
  systemLog(query).then((res) => {
    if (res.code == 200) {
      list.value = res.data.data
      total.value = res.data.count
    }
    loading.value = false
  })
}
const handleSizeChange = (val) => {
  query.page = 1
  query.limit = val
  getList()
}
const handleCurrentChange = (val) => {
  query.page = val
  getList()
}
const onSubmit = () => {
  query.filter = JSON.stringify(searForm)
  query.op = JSON.stringify({ month: '=', content: '%*%' })
  getList()
}
const reset = () => {
  query.page = 1
  searForm.month = ''
  searForm.content = ''
  if (query.filter) delete query.filter
  if (query.op) delete query.op
  getList()
}

nextTick(() => {
  getList()
})
</script>

<style scoped lang="scss">
@import './index';
</style>
